<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" style="width: 60px" />
      <el-form-item label="" prop="deptId" style="width: 220px">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :show-count="true"
          placeholder="请选择归属部门"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="userId">
        <UserSelect
          :userId="queryParams.userId"
          selectType="userName"
          @upData="
            (val) => {
              queryParams.userId = val;
            }
          "
          remote
          reserveKeyword
          placeholder="请选择负责人"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['syf:object:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['syf:object:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['syf:object:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="objectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="排序"
        align="center"
        prop="sortOrder"
        width="150"
      />
      <el-table-column label="对象编码" align="center" prop="code" />
      <el-table-column label="对象名称" align="center" prop="name" />
      <el-table-column
        label="是否重大危险源"
        align="center"
        prop="isMajor"
        width="150"
      >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isMajor == '1'"
            >重大危险源</el-tag
          >
          <el-tag type="success" v-else>非重大危险源</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="负责人" align="center" prop="userName" />

      <el-table-column
        label="停工状态"
        align="center"
        prop="status"
        width="150"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['syf:object:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['syf:object:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改安全分析对象对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="危险源编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入危险源编码" />
        </el-form-item>
        <el-form-item label="对象名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入对象名称" />
        </el-form-item>
        <el-form-item label="是否重大危险源" prop="isMajor">
          <el-radio v-model="form.isMajor" label="1">重大危险源</el-radio>
          <el-radio v-model="form.isMajor" label="2">非重大危险源</el-radio>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <treeselect
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="userId">
          <UserSelect
            :userId="form.userId"
            :deptId="form.deptId"
            @upData="
              (val) => {
                form.userId = val;
              }
            "
            placeholder="请选择负责人"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="sortOrder" label="排序:">
              <el-input-number
                v-model="form.sortOrder"
                controls-position="right"
                :min="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UserSelect from "@/components/syf/UserSelect/index";
import {
  listObject,
  getObject,
  delObject,
  addObject,
  updateObject,
  upObjectStatus,
} from "@/api/syf/risk/object";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Object",
  components: { Treeselect, UserSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全分析对象表格数据
      objectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        deptId: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "危险源编码不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "对象名称不能为空", trigger: "blur" },
        ],
        isMajor: [
          { required: true, message: "请选择是否重大危险源", trigger: "blur" },
        ],
        deptId: [{ required: true, message: "部门不能为空", trigger: "blur" }],
        userId: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询安全分析对象列表 */
    getList() {
      this.loading = true;
      listObject(this.queryParams).then((response) => {
        this.objectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        isMajor: null,
        sortOrder: null,
        status: null,
        deptId: null,
        userId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        firmId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.status = true;
      this.form.isMajor = "1";
      this.title = "添加安全分析对象";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getObject(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安全分析对象";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateObject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addObject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //修改状态
    handleStatusChange(row) {
      if (!row.status) {
        this.$prompt("请输入停工原因", "停工", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
        }).then(({ value }) => {
          upObjectStatus(row.id, value).then((res) => {
            this.$message({
              message: "停用安全分析对象成功！",
              type: "success",
            });
          });
        });
      } else {
        upObjectStatus(row.id, null).then((res) => {
          if (res.data.data) {
            this.$message({
              message: "启用安全分析对象成功！",
              type: "success",
            });
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除安全分析对象编号为"' + ids + '"的数据项？')
        .then(function () {
          return delObject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
